package com.dreamlin.gankio.mvvm.home.feature

import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.api.load
import com.dreamlin.gankio.R
import com.dreamlin.gankio.banner.BannerImageAdapter
import com.dreamlin.gankio.base.ui.*
import com.dreamlin.gankio.common.isFastDoubleClick
import com.dreamlin.gankio.common.jump
import com.dreamlin.gankio.mvvm.home.GankEntity
import com.dreamlin.gankio.webview.WebActivity
import com.youth.banner.indicator.RectangleIndicator
import kotlinx.android.synthetic.main.fragment_feature.*

/**
 * <p> Title: FeatureFragment </p>
 * <p> Description: </p>
 * @author:  dreamlin
 * @date:    9/20/20
 * @version: V1.0.0
 * Created by dreamlin on 9/20/20.
 */

const val pageSize = 20
var itemViewPool: RecyclerView.RecycledViewPool? = null

class FeatureFragment : BaseFragment<,FeatureViewModel>(), BannerImageAdapter.ImageClickListener,
    AdapterListener<GankEntity>, ItemClickListener<GankEntity> {
    override val layout: Int
        get() = R.layout.fragment_feature
    private lateinit var adapter: DiffBaseAdapter<GankEntity>
    private var entities = arrayListOf<GankEntity>()

    override fun initViews() {
        rvItems.isNestedScrollingEnabled = false
        rvItems.setHasFixedSize(true)
        rvItems.layoutManager = LinearLayoutManager(activity)
        if (itemViewPool == null) {
            itemViewPool = rvItems.recycledViewPool
        } else {
            rvItems.setRecycledViewPool(itemViewPool)
        }
        adapter = DiffBaseAdapter(entities, R.layout.item_gankhuo)
        adapter.setListener(this)
        adapter.setItemClickListener(this)
        rvItems.adapter = adapter
    }

    override fun initData() {
        super.initData()
        viewModel.banners.observe(this, Observer {
            banner.addBannerLifecycleObserver(this)
                .setAdapter(BannerImageAdapter(it, this))
                .setIndicator(RectangleIndicator(context))
        })
        viewModel.listBanners()
        viewModel.gankEntities.observe(this, Observer {
            if (it.isNotEmpty()) {
                entities.addAll(it)
                adapter.notifyDataSetChanged()
            }
        })
        viewModel.listHots(pageSize)
    }

    override fun convert(holder: BaseViewHolder<GankEntity>, bean: GankEntity) {
        val iv = holder.getView<ImageView>(R.id.image)
        val ivCategory = holder.getView<ImageView>(R.id.ivCategory)
        if (bean.images.isNotEmpty()) {
            iv.load(bean.images[0])
        }
        holder.setText(R.id.tvTag, bean.type)
        holder.setText(R.id.tvHeading, bean.title)
        holder.setText(R.id.tvDescription, bean.desc)
        holder.setText(R.id.tvAuthor, bean.author)
        val split = bean.createdAt.split(' ')
        if (split.isNotEmpty()) {
            holder.setText(R.id.tvDate, split[0])
        }
    }

    override fun onItemClick(view: View, bean: GankEntity) {
        if (!isFastDoubleClick(view.id)) {
            jump(requireContext(), WebActivity::class.java, bean.title, 0, bean.url)
        }
    }

    override fun onClick(imageView: ImageView, data: BannerModel) {

    }

    override fun click(v: View) {

    }

    override fun onDestroyView() {
        entities.clear()
        super.onDestroyView()
    }

    override fun getVM(): Class<FeatureViewModel> {
        return FeatureViewModel::class.java
    }


}